import base64
import warnings
from functools import wraps

from django.http import JsonResponse
from django.http import HttpResponse

def bs64_encoded(args):
    # 以执行的变法格式变大字符串
    utf8_str = str(args).encode("utf-8")
    # base64加密
    base64_str = base64.b64encode(utf8_str).decode(
        "utf-8")  # base64.b643encoded(utf8_str)是字节格式，使用decode("utf-8")是将其转换成字符串
    return base64_str   # 返回为小写，需要转大写后面添加.upper

def bs64_decoded(encoded_args):
    decoded_data = base64.b64decode(encoded_args).decode("utf-8")
    return decoded_data


def cookie_base64_encoded_required(func):
    @wraps(func)
    def inner(*args, **kwargs):
        try:
            request = args[0]
            if bs64_decoded(request.COOKIES.get('user_id')):
                    func(*args, **kwargs)
            else:
                resp = JsonResponse({"data":False,"msg":"未登录"}, status=200)
                return resp
        except Exception as e:
            resp = JsonResponse({"data": False, "msg": "未登录"}, status=200)
            return resp
    return inner


@cookie_base64_encoded_required
def test_cookies(cookies):
    print('正确响应')

if __name__ == '__main__':
    cookies = "YWRasdtaW4=asasfgasgf"
    test_cookies(cookies)
    # YWRtaW4= admin
    # YWRtaW4xMTE= admin111
    # print(bs64_decoded(cookies))